package org.jeecg.modules.activeschools.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.activeschools.entity.News;
import org.jeecg.modules.activeschools.entity.vo.GetHisCaseReqVO;
import org.jeecg.modules.activeschools.entity.vo.NewsVO;
import org.jeecg.modules.activeschools.mapper.NewsMapper;
import org.jeecg.modules.activeschools.service.INewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;

/**
 * @Description: 文章内容表
 * @Author: jeecg-boot
 * @Date:   2025-03-07
 * @Version: V1.0
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements INewsService {
    @Autowired
    private NewsMapper newsMapper;

    Map<String, Object> map = new HashMap<>();
    @Override
    public Result<?> getNewsByCommon() {
        LambdaQueryWrapper<News> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(News::getTagList, ",66,67,68,");
        List<News> list = newsMapper.selectList(lambdaQueryWrapper);
        if (null != list && list.size() > 0){
            return Result.OK("success", list);
        }
        return Result.error("fail");
    }

    /**
     * @param type 1-新闻中心, 2-通知中心,  3-小学足球教学 4-小学跑步教学 5-小学跳绳教学 6-小学篮球教学
     * @return
     */
    @Override
    public Result<?> getNewsByType(Integer type) {
        switch (type){
            case 1:
                map = getNewsCenter(type);
                break;
            case 2:
                map = getNewsCenter(type);
                break;
            case 6:
                map = getNewsCenter(type);
                break;
            case 3:
                map = getTeachByType(type);
                break;
            case 4:
                map = getTeachByType(type);
                break;
            case 5:
                map = getTeachByType(type);
                break;
            default:
                map = null;
        }
        return Result.OK("success", map);
    }

    @Override
    public Result<?> getHisCase(GetHisCaseReqVO reqVO) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        int offset = (reqVO.getPageNo() - 1) * reqVO.getPageSize();
        params.put("offset", offset);
        params.put("limit", reqVO.getPageSize());
        if (null == reqVO.getYear()) {
            Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR);
            params.put("year", year);
        } else {
            params.put("year", reqVO.getYear());
        }
        if (null != reqVO.getType() && !"".equals(reqVO.getType())) {
            params.put("type", reqVO.getType());
        }
        if (null != reqVO.getTitleName() && !"".equals(reqVO.getTitleName())) {
            params.put("titleName", reqVO.getTitleName());
        }
        List<String> ids = null;
        if (null != reqVO.getTagList() && reqVO.getTagList().length > 0) {
            ids = Arrays.asList(reqVO.getTagList());
        }
        map.put("list", newsMapper.getHisCaseList(params, ids));
        map.put("total", newsMapper.getTotal(params, ids));
        map.put("pageNo", reqVO.getPageNo());
        map.put("pageSize", reqVO.getPageSize());
        return Result.OK("success", map);
    }

    /**
     * 分页查找普通新闻
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public Result<?> selectByPage(Integer pageNo, Integer pageSize) {
        Map<String, Object> params = new HashMap<>();
        int offset = (pageNo - 1) * pageSize;
        params.put("offset", offset);
        params.put("limit", pageSize);
        List<NewsVO> list = newsMapper.selectByPage(params);
        int total = getNewsByChannel("普通新闻","").size();
        Map<String, Object> map = new HashMap<>();
        map.put("pageNo", pageNo);
        map.put("pageSize", pageSize);
        map.put("total", total);
        map.put("list", list);
        return Result.OK("success", map);
    }

    public Map<String, Object> getNewsCenter(Integer type){
        map.clear();
        LambdaQueryWrapper<News> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (1 == type) {
            lambdaQueryWrapper.eq(News::getTagList, ",66,67,68,");
        }
        if (2 == type){
            lambdaQueryWrapper.eq(News::getTagList, ",66,67,68,69,70,");
        }
        if (6 == type){
            lambdaQueryWrapper.eq(News::getChannel, "小篮球教学");
        }
        map.put(type.toString(), newsMapper.selectList(lambdaQueryWrapper));
        return map;
    }

    public Map<String, Object> getTeachByType(Integer type){
        map.clear();
        if (3 == type){
            map.put("水平一|基础运动能力", getNewsByChannel("小学足球教学", "水平一|基础运动能力"));
            map.put("水平二|基础足球技术", getNewsByChannel("小学足球教学", "水平二|基础足球技术"));
            map.put("水平三|技能运用与决策", getNewsByChannel("小学足球教学", "水平三|技能运用与决策"));
        }
        if (4 == type){
            map.put("示范课", getNewsByChannel("小学跑步教学", "示范课"));
            map.put("跑步游戏", getNewsByChannel("小学跑步教学", "跑步游戏"));
            map.put("跑步活动", getNewsByChannel("小学跑步教学", "跑步活动"));
            map.put("跑步小知识", getNewsByChannel("小学跑步教学", "跑步小知识"));
        }
        if (5 == type) {
            map.put("跳绳游戏", getNewsByChannel("小学跳绳教学", "跳绳游戏"));
            map.put("活动课", getNewsByChannel("小学跳绳教学", "活动课"));
        }
        return map;
    }

    public List<News> getNewsByChannel(String channel, String subChannel){
        LambdaQueryWrapper<News> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(News::getChannel, channel).eq(News::getSubChannel, subChannel);
        return newsMapper.selectList(lambdaQueryWrapper);
    }

}
